package com.persicadev.khayyam;

import com.persicadev.khayyam.R;
import com.persicadev.khayyam.IPoemDb.Poem;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class RobaeiView extends View {

	private static final String TAG = "RobaeiView";
	
	private Poem mPoem = null;
//	private boolean isChanged = true;
	
//	private Bitmap mBitmap;
	private static Paint mPaint = null;
	private static Path verse1;
	private static Path verse2;
	private static Path verse3;
	private static Path verse4;
	
	public static boolean hasPersian(Context context) {
		boolean hasPersian = true;
		Bitmap b = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
		Canvas c = new Canvas(b);
		String test = context.getResources().getString(R.string.test1)+context.getResources().getString(R.string.test2);
		Paint p = new Paint();
		p.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/BZar.ttf"));
		p.setTextAlign(Align.RIGHT);
		p.setTextSize(100);
		p.setStyle(Style.FILL);
		
		c.drawText(PersianReshape.reshape(test), 100, 100, p);
		
		for (int i=0; i<100; i++) if (b.getPixel(99, i)!=0) return false;
		
		return hasPersian;
	}
	
	public RobaeiView(Context context) {
		super(context);
	}

	public RobaeiView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public RobaeiView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	private static String a(String s) {
		char chars[] = s.toCharArray();
		char re[] = new char[chars.length];
		for (int i=0; i<re.length; i++) {
			re[i] = chars[re.length-i-1];
		}
		return new String(re);
	}

	public void setPoem(Poem poem,Boolean reshape) {
		mPoem = poem;
		if (reshape) {
			poem.getVerse(0).setPart1(a(PersianReshape.reshape(poem.getVerse(0).getPart1())));
			poem.getVerse(0).setPart2(a(PersianReshape.reshape(poem.getVerse(0).getPart2())));
			poem.getVerse(1).setPart1(a(PersianReshape.reshape(poem.getVerse(1).getPart1())));
			poem.getVerse(1).setPart2(a(PersianReshape.reshape(poem.getVerse(1).getPart2())));
		}
		else{
		poem.getVerse(0).setPart1(poem.getVerse(0).getPart1());
		poem.getVerse(0).setPart2(poem.getVerse(0).getPart2());
		poem.getVerse(1).setPart1(poem.getVerse(1).getPart1());
		poem.getVerse(1).setPart2(poem.getVerse(1).getPart2());

		}
//		isChanged = true;
		invalidate();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		float length = ((float) canvas.getWidth())*2/3;
		if (mPaint==null) {
			mPaint = new Paint();
			mPaint.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/BZar.ttf"));
			mPaint.setTextSize(((float)canvas.getWidth())/13);
			mPaint.setFakeBoldText(true);
			mPaint.setColor(Color.BLACK); // a54921
			
			if (KhayyamActivity.hasPersian) {
				mPaint.setTextAlign(Align.LEFT);
			}
			else {
				mPaint.setTextAlign(Align.CENTER);
			}
//			mPaint.setStrokeWidth(1);
//			mPaint.setStyle(Style.FILL_AND_STROKE);
//			mPaint.setShadowLayer(1, 1, 1, 0xff000000);
			verse1 = new Path();
			float left =(float)(canvas.getWidth()/3.4);
			
			float top = -mPaint.getFontMetrics().top+((canvas.getHeight()-640)/10)+(canvas.getHeight()/40) ;
			Log.d("pd", top+"");
			verse1.moveTo(left, top);
			verse1.lineTo(left+length, top+length);
			verse2 = new Path(verse1);
			float offset = 8 * (mPaint.getFontMetrics().ascent + mPaint.getFontMetrics().descent);
			verse2.offset(offset, -offset);
			verse4 = new Path();
			verse4.moveTo(canvas.getWidth()-left-length+(canvas.getWidth()/20), canvas.getHeight()-top*2-length);
			verse4.lineTo(canvas.getWidth()-left, canvas.getHeight()-2*top);
			verse3 = new Path(verse4);
			verse3.offset(-offset, offset);
//			mBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Config.ARGB_8888);
		}
		if (mPoem!=null) {
//			if (isChanged) {
//				Canvas newCanvas = new Canvas(mBitmap);
				canvas.drawTextOnPath(mPoem.getVerse(0).getPart1(), verse1, 0, 0, mPaint);
				canvas.drawTextOnPath(mPoem.getVerse(0).getPart2(), verse2, 0, 0, mPaint);
				canvas.drawTextOnPath(mPoem.getVerse(1).getPart1(), verse3, 0, 0, mPaint);
				canvas.drawTextOnPath(mPoem.getVerse(1).getPart2(), verse4, 0, 0, mPaint);
//			}
//			canvas.drawBitmap(mBitmap, 0, 0, null);
		}
//		canvas.drawPath(verse1, mPaint);
//		canvas.drawPath(verse2, mPaint);
//		canvas.drawPath(verse3, mPaint);
//		canvas.drawPath(verse4, mPaint);
	}
	
	

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mPaint = null;
	}
	
}
